<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src\admin\model\code.js</title>
  
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
</head>
<body class="yui3-smart">
 <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
               <a class="navbar-brand mainlogo" href="/static/doc/index.html">
             
            <img alt="" src="../assets/css/logo.png" title="">
            
                
          </a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                 <ul class="nav navbar-nav">
                    
                    <li><a href="/home/index/index">首页</a>
                    </li>
                    
                    <li><a href="/static/doc/index.html">文档</a>
                    </li>
                    
                    <li><a href="/admin/index/index">演示</a>
                    </li>
                    
                    <li><a href="/home/index/log">日志</a>
                    </li>
                    
                </ul>
               <div class="navbar-form navbar-right filterAPi" autocomplete="off">
                <input type="text" id='txtSearchAPI' class="form-control search-query" placeholder="Search for API" />
                 <ul id="filterList" class="filterItems dropdown-menu" role="menu"></ul>
                </div>
            </div>
        </div>
    </nav>
    <div id="sidebar">
    <h3>Modules/Classes</h3>
        <div id="api-tabview-filter">
            <input id='txtSearch' type="search" class="form-control" placeholder="Type to filter Modules/Classes">
        </div>
        <dl id="sidebar_list">
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/admin.controller.html">admin.controller</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/admin.controller.base.html">admin.controller.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.code.html">admin.controller.code</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.index.html">admin.controller.index</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.mob.html">admin.controller.mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/admin.model.html">admin.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/admin.model.code.html">admin.model.code</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.code_list.html">admin.model.code_list</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.groupuser.html">admin.model.groupuser</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.groupuser_add.html">admin.model.groupuser_add</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.log.html">admin.model.log</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.login.html">admin.model.login</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.privilege.html">admin.model.privilege</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.teamuser.html">admin.model.teamuser</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.teamuser_add.html">admin.model.teamuser_add</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.user.html">admin.model.user</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.controller.html">cmpage.controller</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.controller.base.html">cmpage.controller.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.mob.html">cmpage.controller.mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.module.html">cmpage.controller.module</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.page.html">cmpage.controller.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.utils.html">cmpage.controller.utils</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.logic.html">cmpage.logic</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.logic.page.html">cmpage.logic.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.model.html">cmpage.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.model.area.html">cmpage.model.area</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page.html">cmpage.model.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_excel.html">cmpage.model.page_excel</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_lookup.html">cmpage.model.page_lookup</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_mob.html">cmpage.model.page_mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.utils.html">cmpage.model.utils</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.cmpage_global.html">cmpage.cmpage_global</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
        </dl>
</div>
   
    <div class="stdoc-content">
        <!--     <form id="options-form" class="form-inline pull-right">
        Show:
        <label for="api-show-inherited" class="checkbox">
            <input type="checkbox" id="api-show-inherited" checked>
            Inherited
        </label>

        <label for="api-show-protected" class="checkbox">
            <input type="checkbox" id="api-show-protected">
            Protected
        </label>

        <label for="api-show-private" class="checkbox">
            <input type="checkbox" id="api-show-private">
            Private
        </label>
        <label for="api-show-deprecated" class="checkbox">
            <input type="checkbox" id="api-show-deprecated">
            Deprecated
        </label>

    </form>

 -->
        <div class="apidocs">
            <div id="docs-main">
                <div class="content">
                    <div class="page-header">
    <h1>src\admin\model\code.js <small>File</small></h1>
</div>

<div class="file">
    <pre class="prettyprint linenums" id='src_code'>
&#x27;use strict&#x27;;
// +----------------------------------------------------------------------
// | CmPage [ 通用页面框架 ]
// +----------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0
// +----------------------------------------------------------------------
// | Author: defans &lt;defans@sina.cn&gt;
// +----------------------------------------------------------------------
/**
 用户及权限系统模块的model部分，实现了实现了相关的数据操作和逻辑处理

 注意点 :
 1. 用户界面显示的类继承自cmpage/page;
 2. 树形结构的参数设置统一存放于t_code表中；
 3. 账套用户和团队用户的设置相仿，逻辑相似；

 @module admin.model
 */

/**
 * 代码于参数设置的操作类，提供一些操作t_code表的方法
 * @class admin.model.code
 */
export default class extends think.model.base {

    /**
     * 用递归法从t_code缓存中返回所有子节点
     * @method  getTreeList
     * @return {Array}  所有子节点组成的数组
     * @param {int} rootID  根节点的ID
     * @param {bool} [selfContains]   是否加入自身节点
     */
    async getTreeList(rootID, selfContains){
        let codes = await this.getCodes();
        let ret = [];
        for(let codeMd of codes){
            if(selfContains &amp;&amp; codeMd.id == rootID ){  //加入自身
                ret.push(codeMd);
            }else if(codeMd.c_pid == rootID){
                ret.push(codeMd);
                let childs =await this.getChildList(codeMd.id,codes, 1);
                for(let child of childs){ ret.push(child);  }
            }
        }
        return ret;
    }
    async getChildList(pid,codes,depth){
        if((depth ++) &gt;20){
            console.log(&#x27;code.getChildList depth &gt; 20 layer.&#x27;);
            return [];
        }
        let ret = [];
        for(let codeMd of codes){
            if(codeMd.c_pid === pid){
                ret.push(codeMd);
                let childs =await this.getChildList(codeMd.id, codes, depth);
                for(let child of childs){ ret.push(child);  }
            }
        }
        return ret;
    }

    /**
     * 根据参数ID取参数的名称，一般用于页面模块配置中的‘替换’调用: admin/cdoe:getNameById
     * @method  getNameById
     * @return {string}  参数名称
     * @param {int} id  参数ID
     */
    async getNameById(id){
        let codes =await this.getCodes();
        for(let codeMd of codes){
            if(codeMd.id === id){
                return codeMd.c_name;
            }
        }
        return &#x27;&#x27;;
    }
    /**
     * 根据参数ID取参数的记录对象
     * @method  getCodeById
     * @return {Object}  参数对象
     * @param {int} id  参数ID
     */
    async getCodeById(id){
        let codes =await this.getCodes();
        for(let codeMd of codes){
            if(codeMd.id === id){
                return codeMd;
            }
        }
        return {};
    }
    /**
     * 根据父节点ID取参数列表
     * @method  getCodesByPid
     * @return {Array}  参数列表
     * @param {int} pid  父节点ID
     */
    async getCodesByPid(pid){
        let codes =await this.getCodes();
        let ret =[];
        for(let codeMd of codes){
            if(codeMd.c_pid === pid){
                ret.push(codeMd);
            }
        }
        return ret;
    }
    /**
     * 根据根节点ID取参数列表，树状
     * @method  getCodesByRoot
     * @return {Array}  参数列表
     * @param {int} rootID  根节点ID
     */
    async getCodesByRoot(rootID){
        let codes =await this.getCodes();
        let ret =[];
        for(let codeMd of codes){
            if(codeMd.c_root == rootID){
                ret.push(codeMd);
            }
        }
        return ret;
    }
    /**
     * 根据参数值取性别，一般用于页面模块配置中的‘替换’调用: admin/cdoe:getSexName
     * @method  getSexName
     * @return {string}  性别
     * @param {bool} [value]  默认值
     */
    getSexName(value){
        return think.isEmpty(value) ? &#x27;男&#x27;:&#x27;女&#x27;;
    }
    /**
     * 取参数列表，带Parm的这几个方法一般是用户业务相关的参数，根节点ID === 4，用缓存
     * @method  getParms
     * @return {Array}  参数列表
     */
    async getParms(){
        return await think.cache(&quot;codeParms&quot;, () =&gt; {
                return this.getTreeList(4,false);
        });
    }
    /**
     * 根据父节点ID取参数列表
     * @method  getParmsByPid
     * @return {Array}  参数列表
     * @param {int} pid  父节点ID
     */
    async getParmsByPid(pid){
        let parms =await this.getParms();
        //let parentID = parseInt(pid);
        pid = parseInt(pid);
        let ret =[];
        for(let parm of parms){
            if(parm.c_pid === pid){
                ret.push(parm);
            }
        }
        return ret;
    }
    /**
     * 根据父节点的c_object值取参数列表
     * @method  getParmsByPobj
     * @return {Array}  参数列表
     * @param {string} pobj  父节点的c_object
     */
    async getParmsByPobj(pobj){
        let pid =await this.getParmByObj(pobj).id;
        return await this.getParmsByPid(pid);
    }
    /**
     * 根据参数ID取参数的记录对象
     * @method  getParmById
     * @return {Object}  参数对象
     * @param {int} id  参数ID
     */
    async getParmById(id){
        let parms =await this.getParms();
        for(let parm of parms){
            if(parm.id === id){
                return parm;
            }
        }
        return {};
    }
    /**
     * 根据参数的c_object值取参数的记录对象
     * @method  getParmById
     * @return {Object}  参数对象
     * @param {string} obj  参数的c_object
     */
    async getParmByObj(obj){
        let parms = await this.getParms();
        for(let parm of parms){
            if(parm.c_ojbect === obj){
                return parm;
            }
        }
        return {};
    }

    /**
     * 清空t_code表的相关缓存
     * @method  clearCodeCache
     */
    async clearCodeCache(){
        await think.cache(&#x27;codeGroups&#x27;,null);
        await think.cache(&#x27;codeRoles&#x27;,null);
        await think.cache(&#x27;codeStocks&#x27;,null);
        await think.cache(&#x27;codeDepts&#x27;,null);
        await think.cache(&#x27;codeCodes&#x27;,null);
        global.debug(&#x27;code cache is clear!&#x27;)
    }

    /**
     * 取账套列表，树状，可以用于页面模块配置中的‘下拉框选择’调用: admin/cdoe:getGroups
     * @method  getGroups
     * @return {Array}  账套列表
     */
    async getGroups(){
        return await think.cache(&quot;codeGroups&quot;, () =&gt; {
            return this.getTreeList(2,true);
        });
    }

    /**
     * 取角色列表，可以用于页面模块配置中的‘下拉框选择’调用: admin/cdoe:getRoles
     * @method  getRoles
     * @return {Array}  角色列表
     */
    async getRoles(){
        return await think.cache(&quot;codeRoles&quot;, () =&gt; {
            return this.getTreeList(3);
        });
    }

    /**
     * 取仓库列表，可以用于页面模块配置中的‘下拉框选择’调用: admin/cdoe:getStocks
     * @method  getStocks
     * @return {Array}  仓库列表
     */
    async getStocks(){
        return await think.cache(&quot;codeStocks&quot;, () =&gt; {
            return this.getTreeList(6);c
        });
    }

    /**
     * 取部门列表，树状，可以用于页面模块配置中的‘下拉框选择’调用: admin/cdoe:getDepts
     * @method  getDepts
     * @return {Array}  部门列表
     */
    async getDepts(){
        return await think.cache(&quot;codeDepts&quot;, () =&gt; {
            return this.getTreeList(5,true);
        });
    }

    /**
     * 取t_code全表记录，缓存
     * @method  getCodes
     * @return {Array}  t_code记录列表
     */
    async getCodes(){
        return await think.cache(&quot;codeCodes&quot;, () =&gt; {
            return this.query(&#x27;select * from t_code order by  c_pid,c_ucode &#x27;);
        });
    }


}
    </pre>
</div>

                </div>
            </div>
        </div>
    </div>
<a id="gotoTop" class='well well-small' href='#'>
    Top
</a>
<script src="../assets/vendor/jquery/jquery-1.8.2.min.js"></script>
<script src="../assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script src="../assets/js/config.js"></script>
<script src="../assets/js/doc.js"></script>
</body>
</html>
